<?php
namespace App\Models\PrizePayment;

use App\Contracts\Repositories\PrizePaymentRepository;

class PrizePaymentService
{
    public $prizePaymentRepository;

    /**
     * PrizePaymentService constructor.
     * @param $prizePaymentRepository
     */
    public function __construct(PrizePaymentRepository $prizePaymentRepository)
    {
        $this->prizePaymentRepository = $prizePaymentRepository;
    }

    public function insert($prizePayment)
    {
        return $this->prizePaymentRepository->insert($prizePayment);
    }

    public function create($prizePayment)
    {
        return $this->prizePaymentRepository->create($prizePayment);
    }

    public function modify($prizePayment, $conditions)
    {
        return $this->prizePaymentRepository->modify($prizePayment, $conditions);
    }

    public function delete($conditions)
    {
        return $this->prizePaymentRepository->delete($conditions);
    }

    public function find($conditions)
    {
        return $this->prizePaymentRepository->find($conditions);
    }

    public function getPrizePaymentPageList($request, $user_id)
    {
        $start = (int)$request->query->get('offset', 0);
        $limit = (int)$request->query->get('limit', 10);
        $keyword = $request->query->get('search');
        $sort = $request->query->get('sort', 'created_at');
        $order = $request->query->get('order', 'desc');
        $sortArray = [[$sort, $order]];
        $conditions = [
            ['user_id', $user_id]
        ];
        if (!is_null($keyword)) {
            $conditions[] = ['order_name', 'like', '%' . $keyword . '%'];
        }

        $list = $this->prizePaymentRepository->findByConditionsPage($conditions, $sortArray, $limit, $start);
        $list = $list->toArray();
        $total = $this->prizePaymentRepository->countByConditions($conditions);
        return ['total' => $total, 'rows' => $list];
    }
}
